package imse.team1.roaddistance.ws;

import imse.team1.roaddistances.data.*;
import imse.team1.roaddistance.googlemapapi.*;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by IntelliJ IDEA.
 * User: Evgenia
 * Date: 6/18/12
 * Time: 7:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class RoadDistanceCalculationServiceSkeleton implements RoadDistanceCalculationServiceSkeletonInterface{
    public DistancesToWarehouses calcRetailerDistanceToWarehouses
                         (RequestDistanceToWarehouses requestDistanceToWarehouses)
    {
      String retailersCity = requestDistanceToWarehouses.getOrigin();
       CalculateDistanceToWarehouses calcDistObject = new CalculateDistanceToWarehouses();
       TreeMap<String,Double> sortedDistances = new TreeMap<String, Double>(CalculateDistanceToWarehouses.calculateDistancesToWarehouse(retailersCity));
        DistancesToWarehouses dtw = new DistancesToWarehouses();

        Iterator it = sortedDistances.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry pairs = (Map.Entry) it.next();
			DistanceBetweenTwoCities dbtc = new DistanceBetweenTwoCities();
            dbtc.setWarehouseLocation((String)pairs.getKey());
            dbtc.setDistance((Double) pairs.getValue());
            dtw.addDistanceToWarehouse(dbtc);
			it.remove(); // avoids a ConcurrentModificationException
		}
        return dtw;
    }
}
